/**************************************************
Class Name:UserInterface
Author's Name:John Lawniczak
Date:04-21-2010
Description of the class: allows the user to 
	lookup and add entries to the database
***************************************/

import java.util.Scanner;


public class UserInterface {
	private String nextCmd;
	private String params = null;
	private Database database = null;
	private Scanner in = new Scanner(System.in);
	public UserInterface(Database db){
		database = db;
		runUI();
	}
	
	/**************************************************
	Method Name: runUI
	Input to the method: none
	Output(Return value): none
	Brief description of the task: runs the UI called 
		from the constructor
	Author: John Lawniczak
	**************************************************/
	private void runUI(){
		do
		{
			System.out.println("\nenter a command (H for help):");
			nextCmd = in.nextLine();
			if(cmdIsValid(nextCmd)){
				if(nextCmd.equals("H")){
					this.showHelpMessage();
				}else if(nextCmd.equals("P")){
					System.out.print(database);
				}else if(nextCmd.equals("LT")){
					this.lookUpTeleNumber();
				}else if(nextCmd.equals("LA")){
					this.lookUpAddr();
				}else if(nextCmd.equals("LN")){
					this.lookUpName();
				}else if(nextCmd.equals("A")){
					this.addToDatabase();
				}else if(nextCmd.equals("Q")){
					System.out.println("Good Bye");
				}
			}else{
				System.out.println("sorry that was an invalid command\n");
				this.showHelpMessage();
			}
		}while(!nextCmd.equals("Q"));
		in.close();
	}
	
	
	/**************************************************
	Method Name: addtoDataBase
	Input to the method: none
	Output(Return value): none
	Brief description of the task: prompts user for info 
		to create a new entry to the database
	Author: John Lawniczak
	**************************************************/
	private void addToDatabase() {
		String name,address,phoneNumber;
		do{
			name = promptParams("Enter new Entry's name: ");
		}while(!FormatChecker.nameMatchesFormatting(name));
		
		
		do{
			address = promptParams("Enter Entry's Address: ");
		}while(!FormatChecker.addressMatchesFormatting(address));

		
		do{
			phoneNumber = promptParams("Enter Entry's phone Number: ");	
		}while(!FormatChecker.phoneNumberMatchesFormatting(phoneNumber));

		
		try {
			database.addEntry(name+":" + address+": "+ phoneNumber);
		} catch (Exception e) {
			System.err.println("Add to Database error: " + e);
		}
		
	}
	/**************************************************
	Method Name: lookUpName
	Input to the method: none
	Output(Return value): none
	Brief description of the task: prompts, looks up and prints 
		out all entries with the correct name
	Author: John Lawniczak
	**************************************************/
	private void lookUpName() {
		int numFound=0;
		String name = promptParams("Enter name to look up:");
		int [] found = database.findIndicesOfName(name);
		
		//loop through all the entries found and print them out
		for(int i=0; i<found.length;i++){
			if(found[i]!=-1){
				System.out.println(database.getEntry(found[i]));
				numFound++;
			}
		}
		System.out.println(numFound + " entries found with name: " + name);		
	}
	
	/**************************************************
	Method Name: lookUpAddr
	Input to the method:
	Output(Return value): none
	Brief description of the task: prompts, looks up and prints 
		out all entries with the correct address
	Author: John Lawniczak
	**************************************************/
	private void lookUpAddr() {
		int numFound = 0;
		String address = promptParams("Enter address to look up:");
		//store the entries indices that we find in this array
		int [] found = database.findIndicesOfAddress(address);
		
		//loop through the found indices and print them out
		for(int i=0; i<found.length;i++){
			if(found[i]!=-1){
				System.out.println(database.getEntry(found[i]));
				numFound++;
			}
		}
		System.out.println(numFound+ " entries found with Address: " + address);
	}

	/**************************************************
	Method Name: lookUpTeleNumber
	Input to the method:none
	Output(Return value): none
	Brief description of the task: prompts, looks up and prints 
		out all entries with the correct phone Number
	Author: John Lawniczak
	**************************************************/
	private void lookUpTeleNumber() {
		int numFound = 0;
		String teleNum = promptParams("Enter Telephone number to look up:");

		//store the entries indices that we find in this array
		int [] found = database.findIndicesOfPhoneNumber(teleNum);
		
		//loop through the found indices and print them out
		for(int i=0; i<found.length;i++){
			if(found[i]!=-1){
				System.out.println(database.getEntry(found[i]));
				numFound++;
			}
		}
		System.out.println(numFound + " entries found with Phone Number: " + teleNum);
	}
	
	/**************************************************
	Method Name: promptParams
	Input to the method: String message to show the user
	Output(Return value): string the user input
	Brief description of the task: prompts for user input
	 	and returns the input
	Author: John Lawniczak
	**************************************************/
	private String promptParams(String promptTxt) {
		System.out.println(promptTxt);
		return in.nextLine();
	}
	/**************************************************
	Method Name:showhelpMessage 
	Input to the method: none
	Output(Return value): none
	Brief description of the task: shows the user all 
		valid commands to enter
	Author: John Lawniczak
	**************************************************/
	private void showHelpMessage(){
		System.out.println("This is the list of valid commands they are caps sensitive:");
		System.out.println("H  -\tshows this message");
		System.out.println("P  -\tprints all entries in data base");
		System.out.println("LT -\tlooks up and prints all entries with matching telephone number");
		System.out.println("LA -\tlooks up and prints all entries with matching address");
		System.out.println("LN -\tlooks up and prints all entries with matching name");
		System.out.println("A  -\tadds new entry to database");
		System.out.println("Q  -\tquits this program");
	}
	
	/**************************************************
	Method Name: cmdIsValid
	Input to the method: String command to validate
	Output(Return value): boolean
	Brief description of the task: checks to make sure 
		the string passed to it is a valid command
	Author: John Lawniczak
	**************************************************/
	private boolean cmdIsValid(String cmd){
		boolean validity = false;
		if(cmd.equals("P") || cmd.equals("LT") || cmd.equals("LA") || cmd.equals("LN") || cmd.equals("A") || cmd.equals("Q")){
			validity = true;
		}
		return validity;
	}
	
}
